<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Type tests and conversions</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Parameterized-Types.xhtml" title="Parameterized Types"/>
    <link rel="next" href="Objects-Classes-and-Modules.xhtml" title="Object, Classes and Modules"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Type tests and conversions" epub:type="subchapter" id="Type-tests-and-conversions">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Type tests and conversions</h2>
          </div>
        </div>
      </div>
      <p>Scheme defines a number of standard type testing predicates.
For example <code class="literal">(vector? x)</code> is <code class="literal">#t</code> if and only if
<code class="literal">x</code> is a vector.
</p>
      <p>Kawa generalizes this to arbitrary type names:
If <em class="replaceable"><code>T</code></em> is a type-name (that is in scope at compile-time),
then <code class="literal"><em class="replaceable"><code>T</code></em>?</code> is a one-argument function that returns
<code class="literal">#t</code> if the argument is an instance of the type <code class="literal"><em class="replaceable"><code>T</code></em></code>,
and <code class="literal">#f</code> otherwise:
</p>
      <pre class="screen">(gnu.lists.FVector? #(123)) ⇒ #t
(let ((iarr (int[] 10))) (int[]? iarr)) ⇒ #t 
</pre>
      <p>To convert (coerce) the result of an expression <em class="replaceable"><code>value</code></em> to a
type <em class="replaceable"><code>T</code></em> use the syntax: <code class="literal">(-&gt;<em class="replaceable"><code>T</code></em> <em class="replaceable"><code>value</code></em>)</code>.
</p>
      <pre class="screen">(-&gt;float 12) ⇒ 12.0f0
</pre>
      <p>In general:
</p>
      <pre class="screen">(<em class="replaceable"><code>T</code></em>? <em class="replaceable"><code>x</code></em>) ⇒ (instance? <em class="replaceable"><code>x</code></em> <em class="replaceable"><code>T</code></em>)
(-&gt;<em class="replaceable"><code>T</code></em> <em class="replaceable"><code>x</code></em>) ⇒ (as <em class="replaceable"><code>T</code></em> <em class="replaceable"><code>x</code></em>)
</pre>
      <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667871904720" class="indexterm"/> <code class="function">instance?</code> <em class="replaceable"><code>value</code></em> <em class="replaceable"><code>type</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Returns <code class="literal">#t</code> iff <em class="replaceable"><code>value</code></em> is an instance of type <em class="replaceable"><code>type</code></em>.
(Undefined if <em class="replaceable"><code>type</code></em> is a primitive type, such as <code class="literal">int</code>.)
</p>
        </blockquote>
      </div>
      <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667871898752" class="indexterm"/> <code class="function">as</code> <em class="replaceable"><code>type</code></em> <em class="replaceable"><code>value</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Converts or coerces <em class="replaceable"><code>value</code></em> to a value of type <em class="replaceable"><code>type</code></em>.
Throws an exception if that cannot be done.
Not supported for <em class="replaceable"><code>type</code></em> to be a primitive type such as <code class="literal">int</code>.
</p>
        </blockquote>
      </div>
    </section>
    <footer>
      <div class="navfooter">
        <p>
          Up: <a accesskey="u" href="Types.xhtml">Types</a></p>
        <p>
        Previous: <a accesskey="p" href="Parameterized-Types.xhtml">Parameterized Types</a></p>
      </div>
    </footer>
  </body>
</html>
